//
//  OpenIdUtil.m
//  SouthChina
//
//  Created by creator_lq on 15/1/9.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "OpenIdUtil.h"

@implementation OpenIdUtil


+(NSString*)createOpenId:(NSString*)userId appId:(NSString*)appid{

    NSString* encrptDes = [ServerEngine encryptDES:userId key:[self getOpenIdDesKey:appid]];

    NSString * base62 =[self base62Encode:encrptDes];
    
    return base62;
}

+(NSString*)getOpenIdDesKey:(NSString*)appid{
    
    NSString *key = [NSString stringWithFormat:@"%@%@",appid,@"@APP"];
    if (key.length>8) {
        key = [key substringToIndex:8];
    }
    
    return key;
}


+(NSMutableURLRequest*)synCookies:(NSString*)urlStr publicId:(NSString*)publicId{

    NSString *value =[self createOpenId:DE.userId appId:publicId];
    NSLog(@"---cookie--%@",value);
   
////    NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
////    NSString *cookieString = [NSString stringWithFormat:@"%@=%@", @"CSU_P2P_TOKEN", value];
////    [newRequest addValue:cookieString forHTTPHeaderField:@"Cookie"];
//    
//    
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
//    [cookieProperties setObject:@"CSU_P2P_TOKEN" forKey:NSHTTPCookieName];
//    [cookieProperties setObject:value forKey:NSHTTPCookieValue];
//    [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
//    [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
//    [cookieProperties setValue:@"" forKey:NSHTTPCookieDomain];
//    [cookieProperties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
    

//    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
//    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
//    NSMutableArray *cookies = [[NSMutableArray alloc] initWithArray:[storage cookies]];    
//    NSDictionary *headers=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
    [request setValue:value forHTTPHeaderField:@"OpenId"];
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"iOS_CSU", @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
    
//    NSDictionary *properties = [[NSMutableDictionary alloc] init];
//    [properties setValue:@"userId" forKey:NSHTTPCookieValue];
//    [properties setValue:@"BENQGURU.GAIA.USERID" forKey:NSHTTPCookieName];
//    [properties setValue:@"" forKey:NSHTTPCookieDomain];
//    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
//    [properties setValue:@"/" forKey:NSHTTPCookiePath];
//    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
//    NSDictionary *properties1 = [[NSMutableDictionary alloc] init];
//    [properties1 setValue:[LoginViewController getLanguageType:loginInfo.lang] forKey:NSHTTPCookieValue];
//    [properties1 setValue:@"BENGGURU.GAIA.CULTURE_CODE" forKey:NSHTTPCookieName];
//    [properties1 setValue:@"" forKey:NSHTTPCookieDomain];
//    [properties1 setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
//    [properties1 setValue:@"/" forKey:NSHTTPCookiePath];
//    NSHTTPCookie *cookie1 = [[NSHTTPCookie alloc] initWithProperties:properties1];
//    NSArray *cookies=[NSArray arrayWithObjects:cookie,cookie1,nil];
//    NSDictionary *headers=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
//    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[object valueForKey:@"url"]]];
//    [request setValue:[headers objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
//    [webView loadRequest:request];

     return request;
}

+(void)removeCookie{
    
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    
}

+(NSString*)base62Encode:(NSString*)base64Str{
    
    NSMutableString *base62Str = [[NSMutableString alloc] initWithCapacity:base64Str.length*2];
    
    for (int i=0; i<base64Str.length; i++) {
        
        char ch = [base64Str characterAtIndex:i];
        
        switch (ch) {
            case 'i':
                
                [base62Str appendString:@"ii"];
                break;
            case '+':
                
                [base62Str appendString:@"ip"];
                break;
            case '/':
                
                [base62Str appendString:@"is"];
                break;
            case '=':
                
                [base62Str appendString:@"ie"];
                break;
            case ' ':
                
                [base62Str appendString:@"ib"];
                break;
            case '\n':
                
                break;
            default:
                [base62Str appendString:[NSString stringWithFormat:@"%c",ch]];
                break;
        }
    }
    
    return base62Str;
    
}





@end
